package com.bhs.sansong.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.VideoView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.bhs.sansong.R;
import com.bhs.sansong.adapter.MoreVideoAdapter;
import com.bhs.sansong.bean.VideoDataBean;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

public class MoreVideoDialog extends Dialog {
    private VideoView mVideo;
    private ImageView mIvPlay;
    private ImageView mProgressBar;
    private List<VideoDataBean> list;
    private SeekBar seekBar;
    private boolean isDrag;
    private RecyclerView mRvList;
    private MoreVideoAdapter mAdapter;
    private int mPosition = 0;
    private MediaPlayer mediaPlayer;
    private ImageView mIvVoice;
    private boolean isMute = true;

    public MoreVideoDialog(@NonNull Context context, List<VideoDataBean> list) {
        super(context, R.style.MyBottomDialogStyle);
        this.list = list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_more_video);
        //获取当前Activity所在的窗体
        Window dialogWindow = getWindow();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT; //设置Dialog距离底部的距离
        dialogWindow.setAttributes(lp); //将属性设置给窗体
        initView();
        initData();
    }

    private void initData() {
        for (VideoDataBean videoDataBean : list) {
            videoDataBean.setPlay(false);
        }
        list.get(mPosition).setPlay(true);
        mAdapter = new MoreVideoAdapter(list);
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.HORIZONTAL, false));
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser) {
                    mVideo.seekTo(progress); // 用户改变进度时，将视频跳转到对应的位置
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // 当用户开始拖动SeekBar时，暂停视频播放
                mVideo.pause();
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // 当用户停止拖动SeekBar时，继续视频播放
                mVideo.start();
            }
        });
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (mPosition != position) {
                    mIvVoice.setVisibility(View.GONE);
                    list.get(mPosition).setPlay(false);
                    list.get(position).setPlay(true);
                    mPosition = position;
                    mAdapter.notifyDataSetChanged();
                    Uri uri = Uri.parse(list.get(position).getImg_path());
                    //设置视频路径
                    mVideo.setVideoURI(uri);
                    mVideo.start();
                }
            }
        });
    }

    RotateAnimation ta;

    private void initView() {
        mVideo = findViewById(R.id.video);
        mIvPlay = findViewById(R.id.iv_play);
        mProgressBar = findViewById(R.id.ProgressBar);
        seekBar = findViewById(R.id.seekBar);
        mRvList = findViewById(R.id.rv_list);
        mProgressBar.post(new Runnable() {
            @Override
            public void run() {
                ta = new RotateAnimation(0, 360, mProgressBar.getWidth() / 2, mProgressBar.getHeight() / 2);
                // 设置动画播放的时间
                ta.setDuration(2000);
                ta.setRepeatCount(10);
                // 开始播放动画
                mProgressBar.startAnimation(ta);
            }
        });

        Uri uri = Uri.parse(list.get(0).getImg_path());
        //设置视频路径
        mVideo.setVideoURI(uri);
        mVideo.start();
        mIvPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mVideo.isPlaying()) {
                    mVideo.pause();
                    mIvPlay.setImageResource(R.mipmap.ic_stop);
                } else {
                    mVideo.start();
                    mIvPlay.setImageResource(R.mipmap.ic_bofang);
                }
            }
        });

        mVideo.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mIvVoice.setVisibility(View.VISIBLE);
                mediaPlayer = mp;
                if(!isMute){
                    mediaPlayer.setVolume(1,1);
                }else {
                    mediaPlayer.setVolume(0,0);
                }
                seekBar.setMax(mVideo.getDuration());
//                    设置当前进度条的值
                seekBar.setProgress(mVideo.getCurrentPosition());
                ta.cancel();
                mProgressBar.setVisibility(View.GONE);
                handler.postDelayed(runnable, 0);
            }
        });

        mVideo.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
                mIvPlay.setImageResource(R.mipmap.ic_stop);
            }
        });
        mIvVoice = findViewById(R.id.iv_voice);
        mIvVoice.setVisibility(View.GONE);
        mIvVoice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isMute){
                    mIvVoice.setImageResource(R.mipmap.ic_listen);
                    isMute=false;
                    mediaPlayer.setVolume(1,1);
                }else {
                    mIvVoice.setImageResource(R.mipmap.ic_mute);
                    isMute=true;
                    mediaPlayer.setVolume(0,0);
                }
            }
        });
    }

    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
        public void run() {
            if (mVideo.isPlaying()) {
                int current = mVideo.getCurrentPosition();
                seekBar.setProgress(current);
            }
            handler.postDelayed(runnable, 200);
        }
    };
}